package com.mobimagic.lockscreen.fullscreen;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.os.Looper;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.mobimagic.lockscreen.util.OpsCheckUtil;

/**
 * Created by seekting on 2016/3/18.
 */
public class FullScreenDetector {

    private static WindowManager mWindowManager;
    private static List<FullScreenCallBack> mFullScreenCallBacks = new ArrayList<FullScreenCallBack>();
    private static DetectorView mDetectorView;

    public static void checkFullScreen(Context context, FullScreenCallBack fullScreenCallBack) {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            throw new RuntimeException("pls,invoke checkFullScreen in main thread!");
        }

        if (mWindowManager == null) {
            mWindowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        }
        if (mDetectorView == null) {
            mDetectorView = new DetectorView(context.getApplicationContext());
        }
        mFullScreenCallBacks.add(fullScreenCallBack);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_FULLSCREEN;
                //            params.type = WindowManager.LayoutParams.TYPE_PHONE;

        //解决小米系统下，未获取悬浮窗权限时不显示悬浮层的bug
        if (OpsCheckUtil.isOpsAllowedSystemAlterWindow(context)) {
            params.type = WindowManager.LayoutParams.TYPE_PHONE;
        } else {
            params.type = WindowManager.LayoutParams.TYPE_TOAST;
        }
        params.format = PixelFormat.RGBA_8888;
        params.gravity = Gravity.LEFT | Gravity.TOP;
        params.x = 0;
        params.y = 0;
        params.width = 1;
        params.height = 1;
        //        mDetectorView.setBackgroundColor(Color.BLUE);
        if (mDetectorView.getParent() == null) {
            mWindowManager.addView(mDetectorView, params);
        } else {
            mDetectorView.requestLayout();
        }

    }

    private static void onFullScreenDetected(int type) {
        for (FullScreenCallBack fullScreenCallBack : mFullScreenCallBacks) {
            fullScreenCallBack.onFullScreenDetected(type);
        }
        mFullScreenCallBacks.clear();
        mWindowManager.removeView(mDetectorView);

    }

    private static class DetectorView extends View {
        private boolean firstDraw = false;
        int[] out = new int[2];

        public DetectorView(Context context) {
            super(context);
        }

        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
            firstDraw = true;
        }

        private void onFirstDraw() {
            getLocationOnScreen(out);
            boolean fullScreen = out[1] == 0;
            if (fullScreen) {
                FullScreenDetector.onFullScreenDetected(FullScreenCallBack.FULLSCREEN_TYPE);

            } else {
                FullScreenDetector.onFullScreenDetected(FullScreenCallBack.TYPE_NOT_FULLSCREEN);
            }

        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if (firstDraw) {
                onFirstDraw();
                firstDraw = false;
            }
        }
    }

}
